
local localplayer = ac.player.self

class.buff_button = extends(class.button)
{
    name = '状态按钮',
    normal_image = '',
    init = function (self)
        class.button.init(self)
        --角标
        self.overlay = self:child_builder
        {
            parent = self,
            type = 'panel',
            normal_image = [[UI\Widgets\Console\Human\CommandButton\human-button-lvls-overlay.blp]],
            x = self.w*0.5,
            y = self.h*0.5,
            w = self.w*0.5,
            h = self.h*0.5,
        }
        self.overlay.text = self.overlay:add_text('',0,self.overlay.h*0.2,self.overlay.w,self.overlay.h*0.8,8,5)
        self.overlay:hide()
        --显示数字
        self.show_num = self:child_builder {
            parent = self,
            type = 'text',
            font_size = 12,
            outline = true,
            align = 5,
            color = 0xffffcc00,
            shadow_color = 0xff000000,
            shadow_offset_x = 02,
            shadow_offset_y = -2,
        }

        self.cooldown = self:child_builder {
            parent = self,
            type = 'model',
            model = [[UI\Feedback\Cooldown\UI-Cooldown-Indicator.mdx]],
            scales = {self.w / 92,self.h / 67},
            show_flag = false,
            is_loop = false,
            animation = 'stand',
        }

        return self
    end,
    new = function (parent, x, y, w, h,ani_flag)
        local ui = class.buff_button:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
            ani_flag = ani_flag,
        }
        return ui
    end,

    --刷新显示
    refresh = function(self,buff)
        if not buff then
            self:set_normal_image('')
            self.show_num:set_text('')
            self.overlay:hide()
            self.cooldown:hide()
            return
        end

        self:set_normal_image(buff.art)
        --右下角数值
        local num = buff:get_stack()
        if num and num>0 then
            self.overlay.text:set_text(num)
            self.overlay:show()
        else
            self.overlay:hide()
        end



        --冷却数值
        local cool = buff:get_remaining()
        local max = buff.time
        if buff.show_pulse and buff.pulse then
            cool = buff:get_pulse()
            max = buff.pulse
        end
        
        if cool>0 and max>0 and cool~=max then
            if cool<1 and max>3 then
                self.cooldown:set_animation_by_index(1)
                self.cooldown:set_progress(cool)
            else
                self.cooldown:set_animation_by_index(0)
                self.cooldown:set_progress(1 - cool/max)
            end
            self.cooldown:show()
        else
            self.cooldown:hide()
        end

        --显示数字
        local num = buff.show_num or ''
        if buff.show_cool and cool~=max and cool>0 then
            if cool<1 then
                num = ('%0.1f'):format(cool)
            else
                num = ('%0.f'):format(cool)
            end
        end
        self.show_num:set_text(num)
    end
}

